package com.kyecloud.dubbo.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kyecloud.common.model.PageQuery;
import com.kyecloud.common.model.Response;
import com.kyecloud.common.utils.CustomBeanUtils;
import com.kyecloud.dubbo.dao.DemoDao;
import com.kyecloud.dubbo.domain.Demo;
import com.kyecloud.dubbo.dto.DemoDto;
import com.kyecloud.dubbo.service.DemoDubboService;

@Service(version = "v1.0.0")
public class DemoDubboServiceImpl implements DemoDubboService{
	
	private static Logger logger = LoggerFactory.getLogger(DemoDubboServiceImpl.class);
	
	@Autowired
	private DemoDao demoDao;
	
	@Autowired
	private TransactionTemplate transactionTemplate;

	@Override
	public Response<Boolean> createDemo(DemoDto demoDto) {
		return transactionTemplate.execute(new TransactionCallback<Response<Boolean>>() {  
		    @Override  
		    public Response<Boolean> doInTransaction(TransactionStatus transactionStatus) {  
		    	Response<Boolean> response = new Response<Boolean>();
		        try {  
		        	Demo demo = CustomBeanUtils.convertPojo(demoDto, Demo.class);
		    		demoDao.createDemo(demo);
		        } catch (Exception e) {  
		            transactionStatus.isRollbackOnly(); 
		            logger.error("Error: 系统异常，｛｝",e);
					return new Response<Boolean>(500, "系统异常，请稍后再试");
		        }  
		        return response;  
		    }  
		});
	}

	@Override
	public Response<DemoDto> findDemoById(Integer id) {
		Response<DemoDto> response = new Response<DemoDto>();
		DemoDto demoDto;
		try {
			Demo demo = demoDao.findDemoById(id);
			demoDto = CustomBeanUtils.convertPojo(demo, DemoDto.class);
		} catch (Exception e) {
			logger.error("Error: 系统异常，｛｝",e);
			return new Response<DemoDto>(500, "系统异常，请稍后再试");
		}
		response.setCode(200);
		response.setMessage("请求成功");
		response.setResult(demoDto);
		return response;
	}

	@Override
	public Response<PageQuery<DemoDto>> queryList(DemoDto demo) {
		PageQuery<DemoDto> pageQuery = null;
		try {
			Integer pageNum = demo.getPageNum();
			Integer pageSize = demo.getPageSize();
			Page<DemoDto> page = PageHelper.startPage(pageNum, pageSize, true);
			List<Demo> list = demoDao.queryList(demo);
			List<DemoDto> demoDtoList = CustomBeanUtils.convertPojos(list,DemoDto.class);
			pageQuery = new PageQuery<DemoDto>(pageNum, pageSize);
			pageQuery.setTotal((int)page.getTotal());
			pageQuery.setRows(demoDtoList);
		} catch (Exception e) {
			logger.error("Error:分页查询异常，请稍后再试",e);
			return new Response<PageQuery<DemoDto>>(500, "FAIL", "系统异常，请稍后再试");
		}
		return new Response<PageQuery<DemoDto>>(200, "SUCCESS", pageQuery);
	}

}
